package com.example.panpan.orderManage.service;

import com.example.common.utils.IMap;
import com.example.panpan.customerManage.mapper.TcCustomerMapper;
import com.example.panpan.customerManage.model.TcCustomer;
import com.example.panpan.orderManage.mapper.TcOrderMapper;
import com.example.panpan.orderManage.model.TcOrder;
import com.example.panpan.productManage.mapper.TcProductMapper;
import com.example.panpan.productManage.mapper.TcStockRecordMapper;
import com.example.panpan.productManage.model.TcProduct;
import com.example.panpan.productManage.model.TcStockRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderService {
    @Autowired
    private TcOrderMapper tcOrderMapper;
    @Autowired
    private TcProductMapper tcProductMapper;
    @Autowired
    private TcStockRecordMapper tcStockRecordMapper;
    @Autowired
    private TcCustomerMapper tcCustomerMapper;

    public List<IMap> getOrderList(IMap param){
        return tcOrderMapper.getOrderList(param);
    }
    public IMap getOrderInfo(IMap param){
        List<IMap> ret = tcOrderMapper.getOrderList(param);
        return ret.get(0);
    }

    public int updOrder(IMap param){
        int count = Integer.valueOf(param.getString("count"));
        int countChange = count - Integer.valueOf(param.getString("oldCount"));
        //验库存量是否满足修改
        TcProduct tcProduct = tcProductMapper.selectByPrimaryKey(param.getString("productId"));
        int stock = tcProduct.getStock();
        if (countChange > stock){
            return stock;
        }else if (countChange == 0){

        }else {
            tcProduct.setStock(stock - countChange);
            //记录库存变化
            TcStockRecord tcStockRecord = new TcStockRecord();
            SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
            String datetime = tempDate.format(new Date());
            tcStockRecord.setRecordId("r"+datetime);
            tcStockRecord.setProductId(param.getString("productId"));
            tcStockRecord.setStockChange(Math.abs(countChange));
            tcStockRecord.setOperateId(param.getString("updateUser"));
            tcStockRecord.setOperateDate(new Date());
            if (countChange > 0){
                tcStockRecord.setRecordType("1");
            }else {
                tcStockRecord.setRecordType("3");
            }
            tcStockRecord.setState("1");
            tcStockRecordMapper.insert(tcStockRecord);
        }
        //进行修改
        TcOrder tcOrder = new TcOrder();
        tcOrder.setOrderId(param.getString("orderId"));
        tcOrder.setCount(count);
        tcOrder.setAddress(param.getString("address"));
        tcOrder.setUpdateUser(param.getString("updateUser"));
        tcOrder.setUpdateDate(new Date());
        try {
            tcProductMapper.updateByPrimaryKeySelective(tcProduct);
            tcOrderMapper.updateByPrimaryKeySelective(tcOrder);
        }catch (Exception e){
            return -1;
        }
        return 1;
    }
    public int updOrderState(IMap param){
        String state = param.getString("state");
        TcOrder tcOrder = tcOrderMapper.selectByPrimaryKey(param.getString("orderId"));
        tcOrder.setState(state);
        if ("0".equals(state)){//取消订单
            TcProduct tcProduct = tcProductMapper.selectByPrimaryKey(param.getString("productId"));
            int stock = tcProduct.getStock();
            int stockChange = tcOrder.getCount();
            tcProduct.setStock(stock + stockChange);
            tcProductMapper.updateByPrimaryKeySelective(tcProduct);
            TcStockRecord tcStockRecord = new TcStockRecord();
            SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
            String datetime = tempDate.format(new Date());
            tcStockRecord.setRecordId("r"+datetime);
            tcStockRecord.setProductId(param.getString("productId"));
            tcStockRecord.setStockChange(Math.abs(stockChange));
            tcStockRecord.setOperateId(param.getString("updateUser"));
            tcStockRecord.setOperateDate(new Date());
            tcStockRecord.setRecordType("3");
            tcStockRecord.setState("1");
            tcStockRecordMapper.insert(tcStockRecord);
        }
        try {
            tcOrderMapper.updateByPrimaryKeySelective(tcOrder);
        }catch (Exception e){
            return -1;
        }
        return 1;
    }

    public int createOrder(IMap param){
        TcOrder tcOrder = new TcOrder();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcOrder.setOrderId("c"+datetime);
        tcOrder.setCustomerId(param.getString("customerId"));
        tcOrder.setProductId(param.getString("productId"));
        tcOrder.setCount(Integer.valueOf(param.getString("addCount")));
        tcOrder.setCreatUser(param.getString("updateUser"));
        tcOrder.setCreatDate(new Date());
        tcOrder.setState("1");
        //获取默认地址
        TcCustomer tcCustomer = tcCustomerMapper.selectByPrimaryKey(param.getString("customerId"));
        tcOrder.setAddress(tcCustomer.getAddress());
        //记录库存变化
        TcProduct tcProduct = tcProductMapper.selectByPrimaryKey(param.getString("productId"));
        int stock = tcProduct.getStock();
        int stockChange = tcOrder.getCount();
        tcProduct.setStock(stock - stockChange);
        tcProductMapper.updateByPrimaryKeySelective(tcProduct);
        TcStockRecord tcStockRecord = new TcStockRecord();
        tcStockRecord.setRecordId("r"+datetime);
        tcStockRecord.setProductId(param.getString("productId"));
        tcStockRecord.setStockChange(Math.abs(stockChange));
        tcStockRecord.setOperateId(param.getString("updateUser"));
        tcStockRecord.setOperateDate(new Date());
        tcStockRecord.setRecordType("1");
        tcStockRecord.setState("1");
        tcStockRecordMapper.insert(tcStockRecord);
        try {
            tcOrderMapper.insert(tcOrder);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public List<IMap> countSumByAge(String age){
        return tcOrderMapper.countSumByAge(age);
    }
}
